
if (files.cwd().endsWith("脚本")) {
  let _require = require;
  require = function (path) {
      if (path.startsWith("./")) {
          path = path.replace("./", "");
      }
      path = files.cwd() + "/gdt_hb/" + path;
      return _require(path);
  }
}
auto.waitFor();
var storage = storages.create("xiangxiangyuedu");
var utils = require("./utils.js");

 
function launchAppEnhance(app_name) {
  let thread = threads.start(function () {
    toastLog("检查是否有打开权限!");
    while (true) {
      var allow = text("允许").findOne(5000);
      if (allow) {
        sleep(1000)
        toastLog("发现允许授权!");
        allow.click();
        sleep(1000);
        thread.interrupt();
        break;
      }
    }
  });
  sleep(1000);
  var result = launchApp(app_name);
  sleep(3000);
  thread.interrupt();
  return result;
}


function task(){
    var 首页 = text("首页").findOne(10000)
    if(首页){
        首页.click();
    }
    var thisEle = text("开始挖矿").findOne(10000);
    if (thisEle) {
            toastLog("开始挖矿")
            click(thisEle.bounds().centerX(),thisEle.bounds().centerY());
            sleep(2000)
            utils.updateAppProgress(20);
    }
    var thisEle = text("领取收益").findOne(10000);
    if (thisEle) {
            toastLog("领取收益")
            click(thisEle.bounds().centerX(),thisEle.bounds().centerY());
            sleep(9000)
            utils.updateAppProgress(40);
    }
    var thisEle = text("开始挖矿").findOne(10000);
    if (thisEle) {
            toastLog("开始挖矿")
            click(thisEle.bounds().centerX(),thisEle.bounds().centerY());
            sleep(2000)
    } 
}


function main() {
    let open_app = launchAppEnhance("CIPC");
    if (open_app) {
      task()
    }
    else {
      toastLog("CIPC开失败，请确认是否安装并授权打开!");
    }
  }
  
try {
    main();
    utils.updateAppProgress(100);
  } catch (error) {
    toastLog(error);
  }
  
  storage.put("script_running_status", "end");